clear all;clc
load one_diff
addpath(genpath('.'));  %添加工具箱路径
length=size(one_diff,2);
r=round(length/2);
blank=zeros(size(one_diff{1,1}));
for i=1:r
    C=abs(double(one_diff{1,i})-double(one_diff{1,i+1}));
    v(i)=std2(C);k(i)=mean2(C);
    log=C>(3*v(i)+k(i));
    blank=blank+log;
    imshow(C,[]);
end
% figure,
[~,idxmax1]=max(v);v(idxmax1)=0;
[~,idxmax2]=max(v);v(idxmax2)=0;
[~,idxmax3]=max(v);v(idxmax3)=0;
[~,idxmax4]=max(v);v(idxmax4)=0;
[~,idxmax5]=max(v);v(idxmax5)=0;
a=abs(double(one_diff{1,idxmax1})-double(one_diff{1,idxmax1+1}));%subplot(231),imshow(a,[]);
b=abs(double(one_diff{1,idxmax2})-double(one_diff{1,idxmax2+1}));%subplot(232),imshow(b,[]);
c=abs(double(one_diff{1,idxmax3})-double(one_diff{1,idxmax3+1}));%subplot(233),imshow(c,[]);
d=abs(double(one_diff{1,idxmax4})-double(one_diff{1,idxmax4+1}));%subplot(234),imshow(d,[]);
e=abs(double(one_diff{1,idxmax5})-double(one_diff{1,idxmax5+1}));%subplot(235),imshow(e,[]);
% subplot(236),imshow(blank,[]);
ggg=a+b+c+d+e;
%% 写滤波器，过滤一些线条
f_ggg=line_filter(ggg,8,1);%这两个超参数8代表检测框大小，2代表阈值(2~4合适)
% figure,imshow(f_ggg,[]);

%% 形态学操作--膨胀+填坑
d_ggg=bwmorph(f_ggg,'dilate',10);figure,imshow(d_ggg,[]);title('膨胀')
regions=bwmorph(d_ggg,'erode',2);figure,imshow(regions,[]);title('侵蚀')
yyy=regions;
% xxx=bwmorph(f_ggg,'clean',1);%imshow(xxx)
% yyy = imfill(xxx,'holes');%imshow(yyy)

%% 最大连通域
yyyy=maxLianTongYu(yyy);
% yyyy=yyy;
%% 圆心检测法，检测四个角点
location_A=zeros(size(yyyy));
location_B=zeros(size(yyyy));
for i=1:size(yyyy,1)
    for j=1:size(yyyy,2)
% 00原点建系拿出左上，右下顶点
    if yyyy(i,j)==1
        location_A(i,j)=i+j;
        location_B(i,j)=size(yyyy,1)-i+j;
    end
% 左下角建系拿出左下，右上顶点
    end
end
[~,mpos]=max(location_A(:));[row1,col1] = ind2sub(size(location_A),mpos);
location_A(location_A==0)=max(max(location_A));
[~,mpos]=min(location_A(:));[row2,col2] = ind2sub(size(location_A),mpos);
[~,mpos]=max(location_B(:));[row3,col3] = ind2sub(size(location_B),mpos);
location_B(location_B==0)=max(max(location_B));
[~,mpos]=min(location_B(:));[row4,col4] = ind2sub(size(location_B),mpos);

%% 看看准不准--很准
figure,imshow(yyyy),hold on,%title('Proposed RDP to detect video corners')
% imshow(one_diff{1,1},[]),hold on
plot(col2,row2,'o','LineWidth',8);
plot(col3,row3,'o','LineWidth',8);
plot(col4,row4,'o','LineWidth',8);
plot(col1,row1,'o','LineWidth',8);


pix_location=[row1,col1;row2,col2;row3,col3;row4,col4];
save pix_location pix_location

legend('A(x_1,y_1)','B(x_2,y_2)','C(x_3,y_3)','D(x_4,y_4)','Interpreter','Latex');   %右上角标注
%% 角点检测
% AAA=cv.goodFeaturesToTrack(yyy);
% imshow(yyy);hold on
% for data=AAA
%     cc=data{1};
%     plot(cc(1),cc(2),'o','LineWidth',8);
% end

%% 线性拟合

% log=C>(mean2(C)+std2(C));
% lsd1 = cv.LineSegmentDetector('Refine','Standard');
% lsd2 = cv.LineSegmentDetector('Refine','None');
% CC=uint8(C);
% 采用Harris算法
% corners = detectHarrisFeatures(ggg,'MinQuality',0.9);
 % 采用MinEigen算法
% corners = detectMinEigenFeatures(I) 
%% 检测线条
% tic, lines1 = lsd1.detect(CC); toc% line是一个元胞数组，里面每一个量代表一条线
% tic, lines2 = lsd2.detect(CC); toc
% 
% %% 展示线条
% % Show found lines with standard refinement
% drawnLines1 = lsd1.drawSegments(CC, lines1);
% imshow(drawnLines1), title('Standard refinement')
% % Show found lines with no refinement
% drawnLines2 = lsd2.drawSegments(CC, lines2);
% imshow(drawnLines2), title('No refinement')
